// Decompiled by DJ v2.3.3.38 Copyright 2000 Atanas Neshkov  Date: 03/11/2007 6:44:22
// Home Page : http//members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   MasterConnection.java
package ig.ejb.metrica.da.editor.dao.abs;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class MasterConnection {

    public MasterConnection() {
        conectionMaster = null;
    }

    public synchronized Connection getConexion(String datasource) throws NamingException, SQLException, Exception {        
    	//Utilidades.getReportHome();

            /*Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup("java:/" + datasource);
            Connection c = ds.getConnection();
            return c;*/
            
                FileInputStream fis = null;
                try {
                    Properties properties = new Properties();
                    properties.load(fis = new FileInputStream(new File("server/"+datasource.toUpperCase().trim()+"-DS.properties")));
                    Class.forName(properties.getProperty("driver"));
                    return DriverManager.getConnection(properties.getProperty("url"), properties);    
                } catch (Exception e) {
                    System.out.println(" DataSource no encontrado : METRICADB :"+e.toString());
                    throw e;
                }finally{
                    try { fis.close(); } catch (Exception e) {}
                }        
            

    }

        public Connection getConnection() throws Exception {
        try {            
            return getConexion("METRICADB");
        } catch (SQLException ex) {
            throw new Exception( "No se pudo conectar con la Base de Datos maestra, Verifique que la Base de Datos este disponible >" + ex.toString());
        } catch (Exception ex) {
            throw new Exception( "No se pudo conectar con la Base de Datos maestra,Comuniquese con el Porveedor >" + ex.toString());
        }
    }

    public void closeConnectionMaster() {
        try {
            this.conectionMaster.close();
        } catch (Exception e) {
            System.out.println("Error closing ConnectionMaster");
        }
    }

    public static MasterConnection getMasterConnection() {
        if (instacia == null) {
            instacia = new MasterConnection();
            System.out.println("MasterConnection -> Intacia creada...");
        }
        return instacia;
    }
    private Connection conectionMaster;
    private static MasterConnection instacia = null;

    public Connection getConnection(String jndi) throws NamingException, SQLException, Exception {
        return getConexion(jndi);
    }
}
